//
//  LiWuVc.m
//  K-Music
//
//  Created by 林杰良 on 16/1/7.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "LiWuVc.h"
#import "UIImageView+WebCache.h"

@interface LiWuVc ()<UIScrollViewDelegate>{
    UIScrollView*myHscrollview;
    NSMutableArray*liwuData;

}
@property(nonatomic,strong)UICollectionView*ShowCollectionView;

@end

@implementation LiWuVc
-(void)backTheFirstPage{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createrleftBTN{
    UIButton*leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 20, 10, 15);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"top_menu_left_back_ico_1.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backTheFirstPage) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    UILabel *MenuText = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 120, 50)];
    
    MenuText.backgroundColor = [UIColor clearColor];
    
    MenuText.textColor=[UIColor blackColor];
    
    MenuText.textAlignment=NSTextAlignmentCenter;
    
    [MenuText setFont:[UIFont systemFontOfSize:17.0f]];
    
    [MenuText setText:@"礼物"];
    
    self.navigationItem.titleView=MenuText;
    
    
}


#pragma mark liwu
-(void)downloadTheLiWuData:(NSString*)vale
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    //获取所有的贵宾信息
    NSString *intactURL;
    if ([vale isEqualToString:@"liwu"]) {
      intactURL  = [NSString stringWithFormat:@"http://119.29.113.233:8080/api/gifts"];

    }else if([vale isEqualToString:@"beibao"]){
        //创建
        intactURL  = [NSString stringWithFormat:@"http://119.29.113.233:8080/api/gifts/received/user_id/%@",@"31"];
    }
    
    //根据id获取信息
    
    //    NSString *intactURL = [NSString stringWithFormat:@"http://119.29.113.233:8080/api/comments/user/%@",userId];
    
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功礼物GET --> %@", dic);
        liwuData=[NSMutableArray array];
        
        NSLog(@"resultCode:%@",dic[@"resultCode"]);
        NSArray*content;
        if ([vale isEqualToString:@"liwu"]) {
            content=dic[@"body"][@"content"];
        }else if([vale isEqualToString:@"beibao"]){
            
            content=dic[@"body"];
        }
        
      

        if (content.count>0) {
            

        
        for (NSDictionary *guibin_Datas in content) {
            liwu*aa=[[liwu alloc]init];
            
            aa.aid=guibin_Datas[@"id"];
            aa.name=guibin_Datas[@"name"];
            aa.pictureUrl=guibin_Datas[@"pictureUrl"];
            
            aa.price=guibin_Datas[@"price"];
            aa.popularity=guibin_Datas[@"popularity"];
            aa.heat=guibin_Datas[@"heat"];
            aa.heatValidityTime=guibin_Datas[@"heatValidityTime"];
            aa.discount=guibin_Datas[@"discount"];
            
            
            aa.hotdescription=guibin_Datas[@"description"];
            aa.isLuxury=guibin_Datas[@"isLuxury"];
            aa.createdBy=guibin_Datas[@"createdBy"];
            aa.createdDate=guibin_Datas[@"createdDate"];
            aa.lastModifiedBy=guibin_Datas[@"lastModifiedBy"];
            
            aa.lastModifiedDate=guibin_Datas[@"lastModifiedDate"];

            [aa fixedString];
            [liwuData addObject:aa];
            
        }
        
        [_ShowCollectionView reloadData];
            
        }else{
            liwuData=nil;
            [_ShowCollectionView reloadData];

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
        
    }];
    
}
-(void)creatUI{
    
    NSArray*textData;
    textData=[NSArray arrayWithObjects:@"礼物店",@"我的背包",nil];
    UIView *headview=[[UIView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 60)];
    headview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:headview];
    
    
    for (int i=0; i<2; i++) {
        UIButton*topBtn=[[UIButton alloc]initWithFrame:CGRectMake(i*WIDTH/2, 5,WIDTH/2-1, 40-2)];
        topBtn.backgroundColor=[UIColor whiteColor];
        [topBtn setTitle:[NSString stringWithFormat:@"%@",textData[i]] forState:UIControlStateNormal];
        [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [topBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        topBtn.userInteractionEnabled = YES;
        topBtn.tag=i+100;
        [topBtn addTarget:self action:@selector(TopButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [headview addSubview:topBtn];
    }
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _ShowCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, headview.frame.origin.y+headview.frame.size.height, WIDTH  , HEIGHT-124)collectionViewLayout:flowLayout];
    _ShowCollectionView.delegate=self;
    _ShowCollectionView.dataSource=self;
    _ShowCollectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_ShowCollectionView];
    
}

-(void)TopButtonClick:(UIButton*)btn{

    if (btn.tag==100) {
        [self downloadTheLiWuData:@"liwu"];
        
    }else if(btn.tag==101){
        
        //
        [self downloadTheLiWuData:@"beibao"];

        
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createrleftBTN];
    [self creatUI];
    [self downloadTheLiWuData:@"liwu"];
 
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];

}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}





-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return liwuData.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [_ShowCollectionView registerClass:[liwuCell class] forCellWithReuseIdentifier:@"liwuCell"];
    
    liwuCell*cell=(liwuCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"liwuCell" forIndexPath:indexPath];
    
    cell.backgroundColor=[UIColor whiteColor];
    liwu*lw=[[liwu alloc]init];
    lw=liwuData[indexPath.row];
    [cell.dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",lw.pictureUrl]] placeholderImage:nil];
    cell.my_name.text=[NSString stringWithFormat:@"%@",lw.name];
    cell.mylabel.text=[NSString stringWithFormat:@"(%@)",lw.price];
    
    return cell;
}




-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        
    
}


//定义每一个cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(WIDTH/3-6, HEIGHT/4);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
